<?php

namespace App\Http\Controllers;

use App\Column;
use App\Http\Controllers\Controller;
use App\Topic;
use App\User;
use Illuminate\Http\Request;

class TopicController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function index(Request $request) {
		$flag = false;
		$clm = [];
		//筛选条件 : 话题名称, 作者, 栏目, 话题状态, 每页条数
		$columns = Column::get(); //获取栏目清单
		foreach ($columns as $k => $v) {
			$clm[$v->id] = $v->title;
		}
		// dd($clm);
		// $topics = Topic::get();//获取话题清单
		$topics = Topic::where(function ($query) use ($request) {
			if (!empty($request->title)) {
				$query->where('title', 'like', '%' . $request->title . '%');
			}
			if (!empty($request->status)) {
				$query->where('status', $request->status);
			}
			if (!empty($request->column)) {
				$query->where('column_id', $request->column);
			}
			if (!empty($request->author)) {
				$query->where('user_id', $request->author);
			}
			$query->where('status', '<', 3);
		})->paginate($request->input('num', 10));
		return view('admin.topic.index', [
			'title' => '话题列表',
			'topics' => $topics,
			'request' => $request,
			'columns' => $clm,
			'flag' => $flag,
		]);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function create() {
		$columns = Column::get(); //获取栏目清单
		return view('admin.topic.create', ['title' => '话题添加', 'columns' => $columns]);
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @return \Illuminate\Http\Response
	 */
	public function store(Request $request) {
		// dd($request->all());
		$this->validate($request, [
			'title' => 'required|regex:/^\S{6,18}$/',
			'desc' => 'required',
			'pic' => 'required',
		], [
			'title.regex' => '话题名不符合规则',
			'title.required' => '话题名不能为空',
			'pic.required' => '话题logo不能为空',
		]);
		$topic = new Topic;
		$topic->user_id = session('id');
		$topic->title = $request->title;
		$topic->desc = $request->desc;
		$topic->column_id = $request->column;
		$topic->status = 2;
		if ($request->hasFile('pic')) {
			//上传文件夹的路径
			$dirname = './uploads/logo/' . date('Ymd') . '/';
			//获取文件的后缀
			$suffix = $request->file('pic')->getClientOriginalExtension();
			//文件名
			$name = time() . rand(100000, 999999) . '.' . $suffix;
			$request->file('pic')->move($dirname, $name);
			//存入对象中(数据表中)
			$topic->pic = trim($dirname, '.') . $name;
		}
		$res = $topic->save() ? 'success' : 'error';
		return back()->with($res, 1);
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function showDelTopic(Request $request) {
		$flag = true;
		$clm = [];
		//筛选条件 : 话题名称, 作者, 栏目, 话题状态, 每页条数
		$columns = Column::get(); //获取栏目清单
		foreach ($columns as $k => $v) {
			$clm[$v->id] = $v->title;
		}
		// dd($clm);
		// $topics = Topic::get();//获取话题清单
		$topics = Topic::where(function ($query) use ($request) {
			if (!empty($request->title)) {
				$query->where('title', 'like', '%' . $request->title . '%');
			}
			if (!empty($request->status)) {
				$query->where('status', $request->status);
			}
			if (!empty($request->column)) {
				$query->where('column_id', $request->column);
			}
			if (!empty($request->author)) {
				$query->where('user_id', $request->author);
			}
			$query->where('status', '=', 3);
		})->paginate($request->input('num', 10));
		return view('admin.topic.index', [
			'title' => '话题列表',
			'topics' => $topics,
			'request' => $request,
			'columns' => $clm,
			'flag' => $flag,
		]);
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function edit($id) {
		$topic = topic::find($id);
		$user = User::find($topic->user_id)->username;
		$column = Column::find($topic->column_id)->title; //获取栏目

		return view('admin.topic.show', [
			'title' => '话题详情',
			'topic' => $topic,
			'username' => $user,
			'column' => $column,
		]);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function update(Request $request, $id) {
		$topic = Topic::find($id);
		$topic->status = 2;
		if ($topic->save()) {
			return redirect('/admin/topic');
		} else {
			return back();
		}
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return \Illuminate\Http\Response
	 */
	public function destroy($id) {
		$topic = Topic::find($id);
		$topic->status = 3;
		if ($topic->save()) {
			return redirect('/admin/topic');
		} else {
			return back();
		}
	}
}
